<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
//use Jialeo\LaravelSchemaExtend\Schema;

class CreateSysTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create(\App\Utils\Enums\TableEnums::SYS_USER, function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 50)->unique();
            $table->string('password')->default('');
            $table->tinyInteger('status')->default(1);
            $table->rememberToken();
            $table->timestamps();
            $table->softDeletes();
            $table->comment = '系统：用户表';
        });
        Schema::create(\App\Utils\Enums\TableEnums::SYS_MENUS, function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 50)->default('')->unique();
            $table->unsignedInteger('pid')->default(0);
            $table->unsignedInteger('root_id')->default(0);
            $table->tinyInteger('status')->default(1);
            $table->tinyInteger('is_lock_name')->default(0);
            $table->unsignedInteger('order')->default(0);
            $table->string('route', 100)->default('')->unique();
            $table->string('url', 512)->default('');
            $table->string('group', 50)->default('');
            $table->string('guard_name', 50)->default('admin');
            $table->string('remark')->default('');
            $table->timestamps();
            $table->comment = '系统：菜单表';
        });

        $tableNames = config('permission.table_names');
        $columnNames = config('permission.column_names');

        Schema::create($tableNames['roles'], function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('guard_name');
            $table->timestamps();
        });

        Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) {
            $table->unsignedInteger('permission_id');

            $table->string('model_type');
            $table->unsignedBigInteger($columnNames['model_morph_key']);
            $table->index([$columnNames['model_morph_key'], 'model_type', ]);

            $table->foreign('permission_id')
                ->references('id')
                ->on($tableNames['permissions'])
                ->onDelete('cascade');

            $table->primary(['permission_id', $columnNames['model_morph_key'], 'model_type'],
                'model_has_permissions_permission_model_type_primary');
        });

        Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames) {
            $table->unsignedInteger('role_id');

            $table->string('model_type');
            $table->unsignedBigInteger($columnNames['model_morph_key']);
            $table->index([$columnNames['model_morph_key'], 'model_type', ]);

            $table->foreign('role_id')
                ->references('id')
                ->on($tableNames['roles'])
                ->onDelete('cascade');

            $table->primary(['role_id', $columnNames['model_morph_key'], 'model_type'],
                'model_has_roles_role_model_type_primary');
        });

        Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
            $table->unsignedInteger('permission_id');
            $table->unsignedInteger('role_id');

            $table->foreign('permission_id')
                ->references('id')
                ->on($tableNames['permissions'])
                ->onDelete('cascade');

            $table->foreign('role_id')
                ->references('id')
                ->on($tableNames['roles'])
                ->onDelete('cascade');

            $table->primary(['permission_id', 'role_id']);

            app('cache')
                ->store(config('permission.cache.store') != 'default' ? config('permission.cache.store') : null)
                ->forget(config('permission.cache.key'));
        });


        Schema::create(\App\Utils\Enums\TableEnums::SYS_CONFIGS, function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 50)->default('');
            $table->string('key', 100)->default('')->unique();
            $table->text('value')->nullable();
            $table->tinyInteger('type')->default(0);
            $table->string('group', 50)->default('');
            $table->string('remark')->default('');
            $table->timestamps();
            $table->softDeletes();
            $table->comment = '系统：配置表';
        });


        Schema::create(\App\Utils\Enums\TableEnums::SYS_LOGS, function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('user_id')->default(0);
            $table->string('user_name', 50)->default('');
            $table->string('url')->default('');
            $table->text('data')->nullable();
            $table->string('ip', 15)->default('');
            $table->string('ua')->default('');
            $table->tinyInteger('type')->default(0);
            $table->timestamps();
            $table->softDeletes();
            $table->comment = '系统：日志表';
        });



        Schema::create(\App\Utils\Enums\TableEnums::SYS_ENTITIES, function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 50)->default('');
            $table->string('table_name', 64)->default('')->unique();
            $table->string('description')->default('');
            $table->string('type')->default('document:文档基础模型 other:其他自定义模型');
            $table->unsignedInteger('is_internal')->default(0);
            $table->unsignedInteger('enable_comment')->default(0);
            $table->unsignedTinyInteger('is_show_content_manage')->default(1);
            $table->string('list_show_field', 2048)->default('title=标题')->comment();
            $table->timestamps();
            $table->softDeletes();
            $table->comment = '系统：模型表';
        });

        Schema::create(\App\Utils\Enums\TableEnums::SYS_ENTITY_FIELDS, function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 64)->default('');
            $table->string('type')->default('');
            $table->string('comment', 100)->default('');
            $table->string('default_value')->default('');
            $table->string('form_name', 20)->default('');
            $table->string('form_type')->default('');
            $table->string('form_comment', 100)->default('');
            $table->string('form_params', 1024)->default('');
            $table->unsignedTinyInteger('is_show')->default(1);
            $table->unsignedTinyInteger('is_show_inline')->default(0);
            $table->unsignedTinyInteger('is_edit')->default(1);
            $table->unsignedTinyInteger('is_required')->default(0);
            $table->unsignedInteger('entity_id')->default(0);
            $table->unsignedInteger('order')->default(77);
            $table->string('form_default_value')->default('')->comment('字段表单默认值');
            $table->timestamps();

            $table->unique(['entity_id', 'name']);
            $table->softDeletes();
            $table->comment = '系统：模型字段表';
        });

        Schema::create(\App\Utils\Enums\TableEnums::SYS_TEMPLATES, function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 50)->unique()->default('');
            $table->string('group', 50)->default('');
            $table->text('content');
            $table->timestamps();
            $table->softDeletes();
            $table->comment = '系统：模板表';
        });

        Schema::create(\App\Utils\Enums\TableEnums::SYS_SENSITIVE_WORDS, function (Blueprint $table) {
            $table->increments('id');
            $table->string('type')->default('');
            $table->string('noun')->default('');
            $table->string('verb')->default('');
            $table->string('exclusive')->default('');
            $table->timestamps();
            $table->softDeletes();
            $table->comment = '系统：敏感词表';
        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists(\App\Utils\Enums\TableEnums::SYS_USER);
        Schema::dropIfExists(\App\Utils\Enums\TableEnums::SYS_MENUS);

        $tableNames = config('permission.table_names');
        Schema::drop($tableNames['role_has_permissions']);
        Schema::drop($tableNames['model_has_roles']);
        Schema::drop($tableNames['model_has_permissions']);
        Schema::drop($tableNames['roles']);

        Schema::dropIfExists(\App\Utils\Enums\TableEnums::SYS_CONFIGS);
        Schema::dropIfExists(\App\Utils\Enums\TableEnums::SYS_LOGS);


        Schema::dropIfExists(\App\Utils\Enums\TableEnums::SYS_ENTITIES);
        Schema::dropIfExists(\App\Utils\Enums\TableEnums::SYS_ENTITY_FIELDS);
        Schema::dropIfExists(\App\Utils\Enums\TableEnums::SYS_TEMPLATES);
        Schema::dropIfExists(\App\Utils\Enums\TableEnums::SYS_SENSITIVE_WORDS);
    }
}
